#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/shm.h>
#include <pthread.h>
#include "mypthread.h"
#include "serial.h"
#include "zigbee.h"
#include "my_camera.h"
#include "server.h"

static int zgbfd;
static pthread_mutex_t cam_mutex;

void *pthread_cam(void *arg)
{
    while (1)
    {
        camera_start();
        sleep(2);
    }
}
void *pthread_zgb(void *arg)
{
    int i = 0;
AA:	
    zgbfd = serial_init();
    if (zgbfd == -1)
    {
        sleep(2);
        i = 1;
        goto AA;
    }
    else
        fprintf(stdout, "init zigbee success\n");
    while (1) 
    {
        int ret = zigbee_get_dat(zgbfd);
        if (ret < 0)
        {
            perror("env_recv:");
        }
        
        printf("------\n");
        
        sleep(1);
    }
}
void *pthread_cli(void *arg)
{
    int *tempfd;
    int connfd;
    pthread_t requst_tid;

    int s = server_init();
    if(0>s)
    {
        printf("init_server failed!\n");
        pthread_exit(NULL);
    }

    tempfd = malloc(sizeof(int));
    if (!tempfd) 
    {
        fprintf(stderr, "server->malloc: malloc failure\n");
        //close(connfd);
        close(s);
    }
    *tempfd = s;
   
    int ret = pthread_create(&requst_tid, NULL, Read_request, tempfd);
    if (ret) 
    {
        errno = ret;
        perror("server->thread");
        //close(connfd);
        close(s);
    } 
    else
        printf("server->thread: create client thread success\n");
        
}

void *Read_request(void *arg)
{
    int ret;
    int connfd = *(int *)arg;
    char request[32] = {0};
    char response[128] = {0};
    
    while(1)
    {
        memset(request, 0, sizeof(request));
        ret = server_recv(connfd, request, sizeof(request));
        if (ret <= 0)
            return (void *)-1;
        //fprintf(stdout, "%s\n", request);
        printf("requeset:%s\n",request);
        // if (strstr(request, "pic") != NULL)
        // {
        //         pthread_cam(&connfd);
        // } 
        if(strstr(request,"p_close") != NULL)
        {
            printf("over1\n");
            close(pic_fd);
            pic_fd = -1;
            printf("fd = %d\n",pic_fd);
        }
        else if (strstr(request, "env") != NULL)
        {
            memset(response, 0, sizeof(response));
            snprintf(response, sizeof(response), "%dt%dh%dl",  data.tem, data.hum, data.light);
            
            //printf("####%dt%dh%dl\n",  data.tem, data.hum, data.light);

            int ret = server_send(connfd, response, sizeof(response));
            if (ret != sizeof(response)) 
            {
                fprintf(stderr, "server->write: send response failed\n");
                sleep(2);
                continue;
            }
            memset(request, 0, sizeof(request));
        }
        else 
        {
            zigbee_exe_cmd(zgbfd,  request);
            memset(request, 0, sizeof(request));
        }
        printf("\n");
    }
    
}
